package cn.wzy.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.wzy.common.CodeMsg;
import cn.wzy.common.Constant;
import cn.wzy.common.PageInfo;
import cn.wzy.common.Result;
import cn.wzy.dao.CustomerDao;
import cn.wzy.dao.VisitLogDao;
import cn.wzy.pojo.Customer;
import cn.wzy.pojo.VisitLog;
import cn.wzy.service.CustomerService;

import java.util.Map;

/**
 * @author nihaowzy
 * @date 2020/12/24 11:57
 */
public class CustomerServiceImpl implements CustomerService {
    CustomerDao customerDao = new CustomerDao();
    VisitLogDao visitLogDao = new VisitLogDao();
    @Override
    public Result queryPage(Map<String, String> params, Integer page, Integer limit) throws Exception {
        PageInfo<Customer> data = customerDao.selectPage(params, page, limit);
        Result result = new Result(data);
        return result;
    }

    @Override
    public Result addCustomer(String name, String phone, Integer sex, String company, Integer userId) throws Exception {

        Customer customer = customerDao.selectByPhone(phone);
        if (customer !=null ){
            return new Result(CodeMsg.CUSTOMER_PHONE_EXIST_ERROR);
        }

        customerDao.insertCustomer(name,phone,sex,company,userId);
        return new Result();
    }

    @Override
    public Result updateCustomer(String name, String phone, Integer sex, String company, Integer id) throws Exception {
        Customer customer = customerDao.selectByPhone(phone);
        //客户存在 且两个客户的ID 不一致  说明其它客户也使用该手机号
        if (customer != null && customer.getId().equals(id)){
            return new Result(CodeMsg.CUSTOMER_PHONE_EXIST_ERROR);
        }

        customerDao.updateCustomer(name,phone,sex,company,id);

        return new Result();

    }

    @Override
    public Result deleteCustomer(Integer id) throws Exception {
       customerDao.deleteCustomer(Constant.CUSTOMER_DELETE_ED,id);
        return new Result();
    }

    @Override
    public Result batchSalesMan(String userId, String[] cIds) throws Exception {
        customerDao.updateSaleMan(userId,cIds);
        return  new Result();
    }

    @Override
    public Result addVisitLog(String customerId, String customerName, Integer userId, String realname, String visitTime, String descp) throws Exception {
        VisitLog visitLog = new VisitLog();
        visitLog.setUserId(userId);
        visitLog.setRealname(realname);
        visitLog.setCustomerId(Integer.parseInt(customerId));
        visitLog.setName(customerName);
        visitLog.setVisitTime(DateUtil.parse(visitTime,"yyyy-MM-dd"));
        visitLogDao.addVisitLog(visitLog);

        return new Result();
    }


}
